package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.vo.WorkProcedureVO;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.WorkingProcedure;
import com.ruoyi.base.service.IWorkingProcedureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工序管理
 * 
 * @author ruoyi
 * @date 2022-08-17
 */
@RestController
@RequestMapping("/base/procedure")
public class WorkingProcedureController extends BaseController
{
    @Autowired
    private IWorkingProcedureService workingProcedureService;

    /**
     * 查询工序列表
     */
    @ApiOperation("查询工序列表")
    @PreAuthorize("@ss.hasPermi('base:procedure:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkingProcedure workingProcedure)
    {
        startPage();
        List<WorkProcedureVO> list = workingProcedureService.selectWorkingProcedureList(workingProcedure);
        return getDataTable(list);
    }

    /**
     * 导出工序列表
     */
    @ApiOperation("导出工序列表")
    @PreAuthorize("@ss.hasPermi('base:procedure:export')")
    @Log(title = "工序", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkingProcedure workingProcedure)
    {
        List<WorkProcedureVO> list = workingProcedureService.selectWorkingProcedureList(workingProcedure);
        ExcelUtil<WorkProcedureVO> util = new ExcelUtil<WorkProcedureVO>(WorkProcedureVO.class);
        util.exportExcel(response, list, "工序数据");
    }

    /**
     * 获取工序详细信息
     */
    @ApiOperation("获取工序详细信息")
    @PreAuthorize("@ss.hasPermi('base:procedure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(workingProcedureService.selectWorkingProcedureById(id));
    }

    /**
     * 新增工序
     */
    @ApiOperation("新增工序")
    @PreAuthorize("@ss.hasPermi('base:procedure:add')")
    @Log(title = "工序", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkProcedureVO workProcedureVO) {
        if (StringUtils.isBlank(workProcedureVO.getProcedureName())) {
            return AjaxResult.error("工序名称不能为空!");
        }
        if (StringUtils.isBlank(workProcedureVO.getProcedureClassifyNo())) {
            return AjaxResult.error("工序分类不能为空!");
        }
        return toAjax(workingProcedureService.insertWorkingProcedure(workProcedureVO));
    }

    /**
     * 修改工序
     */
    @ApiOperation("修改工序")
    @PreAuthorize("@ss.hasPermi('base:procedure:edit')")
    @Log(title = "工序", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkProcedureVO workProcedureVO) {
        return toAjax(workingProcedureService.updateWorkingProcedure(workProcedureVO));
    }

    /**
     * 删除工序
     */
    @ApiOperation("删除工序")
    @PreAuthorize("@ss.hasPermi('base:procedure:remove')")
    @Log(title = "工序", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(workingProcedureService.deleteWorkingProcedureByIds(ids));
    }

}
